package cool.webstudy.admin.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cool.webstudy.admin.model.dto.user.CreateUserDTO;
import cool.webstudy.admin.model.dto.user.PaginationQueryUserInfoListDTO;
import cool.webstudy.admin.model.dto.user.UpdateUserDTO;
import cool.webstudy.admin.model.dto.user.UserLoginDTO;
import cool.webstudy.admin.model.vo.user.PaginationQueryUserInfoListVO;
import cool.webstudy.admin.model.vo.user.QueryCurrentUserVO;
import cool.webstudy.admin.model.vo.user.QueryUserDetailInfoVO;
import cool.webstudy.admin.model.vo.user.QueryUserRoughInfoVO;
import cool.webstudy.admin.service.UserService;
import cool.webstudy.admin.utils.aspect.logger.operation.ApiOperationLog;
import cool.webstudy.admin.utils.aspect.request.RequestParameterCheck;
import cool.webstudy.admin.utils.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户管理")
@RestController
@RequestMapping("/user")
@RequestParameterCheck
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @Operation(summary = "用户登录接口")
    @ApiOperationLog(description = "用户登录接口")
    public Result login(@RequestBody UserLoginDTO dto) {
        String token = userService.login(dto);
        return Result.success(token);
    }

    @GetMapping("/current-user")
    @Operation(summary = "获取当前用户信息接口")
    @ApiOperationLog(description = "获取当前用户信息接口")
    public Result queryCurrentUser() {
        QueryCurrentUserVO currentUser = userService.queryCurrentUser();
        return Result.success(currentUser);
    }

    @GetMapping("/query/detail-info")
    @Operation(summary = "获取用户详细信息接口")
    public Result queryDetailInfo(@RequestParam String unCode) {
        QueryUserDetailInfoVO vo = userService.queryUserDetailInfo(unCode);
        return Result.success(vo);
    }

    @PostMapping("/create")
    @Operation(summary = "创建用户接口")
    public Result create(@RequestBody CreateUserDTO dto) {
        userService.createUser(dto);
        return Result.success();
    }

    @PostMapping("/update")
    @Operation(summary = "更新用户接口")
    public Result update(@RequestBody UpdateUserDTO dto) {
        userService.updateUser(dto);
        return Result.success();
    }

    @PostMapping("/batch/delete")
    @Operation(summary = "批量删除用户接口")
    public Result batchDelete(@RequestBody List<String> unCodeList) {
        userService.batchDeleteUser(unCodeList);
        return Result.success();
    }

    @PostMapping("/page-query/list")
    @Operation(summary = "分页获取用户信息列表接口")
    public Result<Page<PaginationQueryUserInfoListVO>> paginationQueryUserInfoList(@RequestBody PaginationQueryUserInfoListDTO dto) {
        Page<PaginationQueryUserInfoListVO> vo = userService.paginationQueryUserInfoList(dto);
        return Result.success(vo);
    }

    @GetMapping("/query/enabled/rough-info/list")
    @Operation(summary = "根据用户名或者账号获取启用用户粗略信息列表")
    public Result queryEnabledRoughInfo(@RequestParam(required = false) String context) {
        List<QueryUserRoughInfoVO> vo = userService.queryEnabledUserRoughInfoByUserNameOrAccount(context);
        return Result.success(vo);
    }
}
